//
//  CZNavigationViewController.swift
//  WeiBo
//
//  Created by Apple on 16/9/4.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  导航控制器基类
class CZNavigationViewController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置边缘控制器代理
        interactivePopGestureRecognizer?.delegate = self
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //  如果导航控制器已经有一个视图控制器了，那么再push过来的视图控制器隐藏底部tabbar
        if viewControllers.count == 1 {
            //  让tabbar隐藏
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: true)
        
        if viewControllers.count > 2 {
            //  如果是我们关系的类型 则表情改成返回
            if viewController.isKind(of: CZTestViewController.self) {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
            
        } else {
            //  控制器是跟视图控制器的时候显示上级标题
            //  如果是我们关系的类型 则表情改成返回
            if viewController.isKind(of: CZTestViewController.self) {
                
                //  获取首页标题
                let title = viewControllers.first!.title!
                //  设置左侧按钮就没有边缘手势了
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title:title, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
        }
        
        
    }
    
    @objc fileprivate func popAction() {
        popViewController(animated: true)
    }
    
    // MARK: --边缘手势

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count == 1 {
            //  只有一个控制器就不接受边缘手势
            return false
        }
        return true
    }

}
